package com.lngsyz.ywtb.biz.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lngsyz.ywtb.biz.service.BizService;
import com.lngsyz.ywtb.entity.CommonConstant;
import com.lngsyz.ywtb.entity.LoginInfo;
import com.lngsyz.ywtb.entity.URIResult;
import com.lngsyz.ywtb.entity.biz.AddElecOrder;
import com.lngsyz.ywtb.entity.biz.AddPhyOrder;
import com.lngsyz.ywtb.entity.biz.CurrentInfo;
import com.lngsyz.ywtb.entity.biz.GetWxSignature;
import com.lngsyz.ywtb.entity.biz.LocalCompany;
import com.lngsyz.ywtb.entity.biz.LocalDistrict;
import com.lngsyz.ywtb.entity.biz.OrderDetail;
import com.lngsyz.ywtb.entity.biz.OrderInfo;
import com.lngsyz.ywtb.entity.biz.PriceInfo;
import com.lngsyz.ywtb.entity.biz.ReferPhySeal;
import com.lngsyz.ywtb.entity.biz.YwtbCompany;
import com.lngsyz.ywtb.wx.entity.WxSignature;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

@Controller
@Api(tags = "BizController", description = "业务接口")
@RequestMapping("v1/biz")
public class BizController {

	@Autowired()
	private BizService bizService;

	@ApiOperation(value = "获取当前信息", notes = "获取当前信息")
	@RequestMapping(value = "/getCurrentInfo", method = { RequestMethod.GET })
	@ResponseBody
	public URIResult<CurrentInfo> getCurrentInfo(
			@ApiIgnore LoginInfo loginInfo) {
		URIResult<CurrentInfo> res = new URIResult<CurrentInfo>();
		try {
			CurrentInfo data = bizService.getCurrentInfo(loginInfo);
			res.setData(data);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}

	@ApiOperation(value = "获取全市刻章单位列表", notes = "获取全市刻章单位列表")
	@RequestMapping(value = "/getLocalCompanyList", method = { RequestMethod.GET })
	@ResponseBody
	public URIResult<List<LocalCompany>> getLocalCompanyList(
			@ApiParam(name = "district", value = "6位地址区划") @RequestParam(value = "district", defaultValue = "") String district,
			@ApiParam(name = "name", value = "用名字模糊检索") @RequestParam(value = "name", defaultValue = "") String name,
			@ApiParam(name = "page", value = "分页页码", required = true) @RequestParam(value = "page", defaultValue = "0") int page,
			@ApiParam(name = "size", value = "每页显示数量", required = true) @RequestParam(value = "size", defaultValue = "10") int size,
			@ApiIgnore LoginInfo loginInfo) {
		URIResult<List<LocalCompany>> res = new URIResult<List<LocalCompany>>();
		try {
			List<LocalCompany> data = bizService.getLocalCompanyList(district, name, loginInfo, page, size);
			int total = bizService.getLocalCompanyCount(district, name, loginInfo);
			res.setData(data);
			res.setTotal(total);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}
	
	@ApiOperation(value = "获取本市区划（用于刻章单位区划筛选）", notes = "获取本市区划（用于刻章单位区划筛选）")
	@RequestMapping(value = "/getLocalDistrict", method = { RequestMethod.GET })
	@ResponseBody
	public URIResult<List<LocalDistrict>> getLocalDistrict(
			@ApiIgnore LoginInfo loginInfo) {
		URIResult<List<LocalDistrict>> res = new URIResult<List<LocalDistrict>>();
		try {
			List<LocalDistrict> data = bizService.getLocalDistrict(loginInfo);
			res.setData(data);
			res.setTotal(data.size());
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}
	
	@ApiOperation(value = "获取下订单页面刻章厂列表", notes = "获取下订单页面刻章厂列表")
	@RequestMapping(value = "/getYwtbCompany", method = { RequestMethod.GET })
	@ResponseBody
	public URIResult<List<YwtbCompany>> getYwtbCompany(
			@ApiParam(name = "district", value = "6位地址区划") @RequestParam(value = "district", defaultValue = "") String district,
			@ApiParam(name = "name", value = "用名字模糊检索") @RequestParam(value = "name", defaultValue = "") String name,
			@ApiParam(name = "sealMedia", value = "印章介质（1：物理 3：电子）") @RequestParam(value = "sealMedia", defaultValue = "") String sealMedia,
			@ApiIgnore LoginInfo loginInfo) {
		URIResult<List<YwtbCompany>> res = new URIResult<List<YwtbCompany>>();
		try {
			List<YwtbCompany> data = bizService.getYwtbCompany(district, name, sealMedia, loginInfo);
			res.setData(data);
			res.setTotal(data.size());
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}
	
	@ApiOperation(value = "获取刻章单位定价", notes = "获取刻章单位定价")
	@RequestMapping(value = "/getPriceByCompanyId", method = { RequestMethod.GET })
	@ResponseBody
	public URIResult<List<PriceInfo>> getPriceByCompanyId(
			@ApiParam(name = "companyId", value = "刻章单位ID") @RequestParam(value = "companyId", defaultValue = "") String companyId,
			@ApiParam(name = "sealMedia", value = "印章类型（1：物理印章；3：电子印章）") @RequestParam(value = "sealMedia", defaultValue = "") String sealMedia,
			@ApiIgnore LoginInfo loginInfo) {
		URIResult<List<PriceInfo>> res = new URIResult<List<PriceInfo>>();
		try {
			List<PriceInfo> data = bizService.getPriceByCompanyId(companyId, sealMedia);
			res.setData(data);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}

	@ApiOperation(value = "新增物理订单", notes = "新增物理订单")
	@ApiImplicitParam(name = "addPhyOrder", value = "新增物理订单", dataType = "AddPhyOrder", required = true, paramType = "body")
	@RequestMapping(value = "/addPhyOrder", method = RequestMethod.POST)
	@ResponseBody
	public URIResult<String> addPhyOrder(@RequestBody AddPhyOrder addPhyOrder,
			@ApiIgnore LoginInfo loginInfo) {
		URIResult<String> res = new URIResult<String>();
		try {
			String orderId = bizService.addPhyOrder(addPhyOrder, loginInfo);
			res.setData(orderId);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}
	
	@ApiOperation(value = "新增电子订单", notes = "新增电子订单")
	@ApiImplicitParam(name = "addElecOrder", value = "新增电子订单", dataType = "AddElecOrder", required = true, paramType = "body")
	@RequestMapping(value = "/addElecOrder", method = RequestMethod.POST)
	@ResponseBody
	public URIResult<String> addElecOrder(@RequestBody AddElecOrder addElecOrder,
			@ApiIgnore LoginInfo loginInfo) {
		URIResult<String> res = new URIResult<String>();
		try {
			String orderId = bizService.addElecOrder(addElecOrder, loginInfo);
			res.setData(orderId);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}

	@ApiOperation(value = "获取订单详情", notes = "获取订单详情")
	@RequestMapping(value = "/getOrderDetail", method = { RequestMethod.GET })
	@ResponseBody
	public URIResult<OrderDetail> getOrderDetail(
			@ApiParam(name = "id", value = "订单ID") @RequestParam(value = "id", defaultValue = "") String id,
			@ApiIgnore LoginInfo loginInfo) {
		URIResult<OrderDetail> res = new URIResult<OrderDetail>();
		try {
			OrderDetail data = bizService.getOrderDetail(id);
			res.setData(data);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}
	
	@ApiOperation(value = "获取我的订单列表", notes = "获取我的订单列表")
	@RequestMapping(value = "/getOrderList", method = { RequestMethod.GET })
	@ResponseBody
	public URIResult<List<OrderInfo>> getOrderList(
			@ApiParam(name = "pay", value = "0：待支付；1：已支付；8：已退款；9：订单已取消") @RequestParam(value = "pay", defaultValue = "") String pay,
			@ApiIgnore LoginInfo loginInfo) {
		URIResult<List<OrderInfo>> res = new URIResult<List<OrderInfo>>();
		try {
			List<OrderInfo> data = bizService.getOrderList(loginInfo, pay);
			res.setTotal(data.size());
			res.setData(data);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}
	
	@ApiOperation(value = "获取订单当前支付状态", notes = "获取订单当前支付状态")
	@RequestMapping(value = "/getPayStatus", method = { RequestMethod.GET })
	@ResponseBody
	public URIResult<String> getPayStatus(
			@ApiParam(name = "id", value = "订单ID") @RequestParam(value = "id", defaultValue = "") String id,
			@ApiIgnore LoginInfo loginInfo) {
		URIResult<String> res = new URIResult<String>();
		try {
			res.setData(bizService.getPayStatus(id));
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}
	
	@ApiOperation(value = "取消订单", notes = "取消订单")
	@RequestMapping(value = "/cancelOrder", method = { RequestMethod.GET })
	@ResponseBody
	public URIResult<String> cancelOrder(
			@ApiParam(name = "id", value = "订单ID") @RequestParam(value = "id", defaultValue = "") String id,
			@ApiIgnore LoginInfo loginInfo) {
		URIResult<String> res = new URIResult<String>();
		try {
			bizService.cancelOrder(id, loginInfo);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}

	@ApiOperation(value = "获取当前用户能查询到的物理印章列表", notes = "获取当前用户能查询到的物理印章列表")
	@RequestMapping(value = "/getReferPhySeal", method = { RequestMethod.GET })
	@ResponseBody
	public URIResult<List<ReferPhySeal>> getReferPhySeal(
			@ApiIgnore LoginInfo loginInfo) {
		URIResult<List<ReferPhySeal>> res = new URIResult<List<ReferPhySeal>>();
		try {
			List<ReferPhySeal> data = bizService.getReferSealList(loginInfo);
			res.setData(data);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}
	
	@ApiOperation(value = "电子印章订单中用于提示信息", notes = "电子印章订单中用于提示信息")
	@RequestMapping(value = "/getReferSealByReferId", method = { RequestMethod.GET })
	@ResponseBody
	public URIResult<ReferPhySeal> getReferSealByReferId(
			@ApiParam(name = "id", value = "关联物理印章的ID") @RequestParam(value = "id", defaultValue = "") String id,
			@ApiIgnore LoginInfo loginInfo) {
		URIResult<ReferPhySeal> res = new URIResult<ReferPhySeal>();
		try {
			ReferPhySeal data = bizService.getReferSealByReferId(id, loginInfo);
			res.setData(data);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}

	@ApiOperation(value = "获取公众号签名", notes = "获取公众号签名")
	@ApiImplicitParam(name = "getWxSignature", value = "新增物理订单", dataType = "GetWxSignature", required = true, paramType = "body")
	@RequestMapping(value = "/getWxSignature", method = RequestMethod.POST)
	@ResponseBody
	public URIResult<WxSignature> getWxSignature(@RequestBody GetWxSignature getWxSignature,
			@ApiIgnore LoginInfo loginInfo) {
		URIResult<WxSignature> res = new URIResult<WxSignature>();
		try {
			res.setData(bizService.getWxSignature(getWxSignature.getUrl()));
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}
}